package com.java.comprehensive.arithmetic.leecode.easy;


import java.util.ArrayList;
import java.util.List;

/**
 * @Author: 窦绍飞
 * @Date: 2022/6/15
 */
public class LeetCode_234 {
	public static boolean isPalindrome(ListNode head) {
		List<Integer> vals = new ArrayList<Integer>();

		// 将链表的值复制到数组中
		ListNode currentNode = head;
		while (currentNode != null) {
			vals.add(currentNode.val);
			currentNode = currentNode.next;
		}

		// 使用双指针判断是否回文
		int front = 0;
		int back = vals.size() - 1;
		while (front < back) {
			if (!vals.get(front).equals(vals.get(back))) {
				return false;
			}
			front++;
			back--;
		}
		return true;
	}

	public static void main(String[] args) {

		ListNode listNode1 = new ListNode();
		listNode1.val = 1;
		ListNode listNode2 = new ListNode();
		listNode2.val = 2;
		ListNode listNode3 = new ListNode();
		listNode3.val = 3;
		ListNode listNode4 = new ListNode();
		listNode4.val = 4;

		listNode1.next = listNode2;
		listNode2.next = listNode3;
		listNode3.next = listNode1;
		System.out.println(isPalindrome(listNode1));
	}

	public static class ListNode {
		int val;
		ListNode next;

		ListNode() {
		}

		ListNode(int val) {
			this.val = val;
		}

		ListNode(int val, ListNode next) {
			this.val = val;
			this.next = next;
		}
	}

}
